package com.srd.phone.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.PreferenceUtils;
import com.srd.phone.utils.UserUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 绑定邮箱
 */
public class SafeBoundEmailActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_email, et_code;
    private Button bt_code, bt_commit;
    private UtilPresenter utilPresenter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_email_bound);

        initView();
        initData();
    }

    @Override
    public void initView() {
        tv_common_title.setText("绑定邮箱");
        et_email = (EditText) findViewById(R.id.et_email);
        bt_code = (Button) findViewById(R.id.bt_get_code);
        et_code = (EditText) findViewById(R.id.et_code);
        bt_commit = (Button) findViewById(R.id.bt_commit);
    }

    @Override
    public void initData() {
        bt_code.setOnClickListener(this);
        bt_commit.setOnClickListener(this);

        presenter = new UtilPresenter(requestQueue, this);
        utilPresenter = (UtilPresenter)presenter;
        setUrlArrs(Constants.VALID_EMAIL, Constants.RE_EM);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_get_code:  // 获取验证码

                String eValue = et_email.getText().toString().trim();
                if(TextUtils.isEmpty(eValue)){
                    toastShow("邮箱不能为空~");
                    return;
                }
                downTimer.start();
                bt_code.setEnabled(false);

                utilPresenter.singleParam(Constants.VALID_EMAIL, "value", eValue);

                break;
            case R.id.bt_commit:  // 提交
                String eCode = et_code.getText().toString().trim();
                String eEmail = et_email.getText().toString().trim();
                if(TextUtils.isEmpty(eEmail)){
                    toastShow("邮箱不能为空~");
                    return;
                }else if(TextUtils.isEmpty(eCode)){
                    toastShow("激活码不能为空~");
                    return;
                }else if(!eCode.equals(returnCode)){
                    toastShow("激活码不正确~");
                    return;
                }

                Map<String, String> map = new HashMap<>();
                map.put("em", eEmail);
                map.put("t", UserUtils.getUserInfo().ticket);  // ticket
//                map.put("t", "3cf12f78-0874-43c1-8666-d6f834aa2fc7");
                utilPresenter.mapParams(Constants.RE_EM, map);
                break;
        }
    }

    CountDownTimer downTimer = new CountDownTimer(1000 * 60, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            bt_code.setText("剩余" + millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            bt_code.setEnabled(true);
            bt_code.setText("重新获取");
        }
    };

    private String returnCode = "";

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        cancelWaitingDialog();
        if(Constants.VALID_EMAIL.equals(tag)){
            try {
                JSONObject jsonObj = new JSONObject(data);
                returnCode = jsonObj.optString("data");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if(Constants.RE_EM.equals(tag)){
            try {
                JSONObject jsonObj = new JSONObject(data);
                PreferenceUtils.getInstance().putData("info.email", et_email.getText().toString().trim());
                toastShow(jsonObj.optString("msg"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        cancelWaitingDialog();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        downTimer.cancel();
    }
}
